$(function () {
    getUserInfo();
    $('#logout').click(function () {
        // alert(1)
        layer.confirm('是否确认退出', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem('token');
            location.href = '/login.html'
        });
    })



    function getUserInfo() {
        $.ajax({
            method: 'GET',
            url: '/my/userinfo',
            // headers: {
            //     Authorization: localStorage.getItem('token') || ''
            // },
            success: function (res) {
                // console.log(res);
                if (res.status != 0) return layui.layer.msg('获取用户信息失败');
                renderAvatar(res.data)
            },
            // complete: function (res) {
            //     console.log(res);
            //     if (res.responseJSON.status === 1 && res.responseJSON.message === "身份认证失败！") {
            //         localStorage.removeItem('token');
            //         location.href = '/login.html'
            //     }
            // }
        })
    }
    function renderAvatar(user) {
        let name = user.nickname || user.username;
        $('#welcome').html('欢迎  ' + name);
        if (user.user_pic) {
            $('.layui-nav-img').attr('src', user.user_pic).show();
        } else {
            $('.layui-nav-img').hide();
            var first = name[0].toUpperCase();
            $('.text-avatar').html(first).show()
        }
    }
})